package game;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class FightTheLandlord extends JFrame {
    private List<Card> pukes = new ArrayList<>();
    private List<Card> player1 = new ArrayList<>();
    private List<Card> player2 = new ArrayList<>();
    private List<Card> player3 = new ArrayList<>();
    private List<Card> base = new ArrayList<>();


    public FightTheLandlord() {
        this.setLayout(null);
        this.init();
        this.xipai();
        this.fapai();
        int y = 600;
        for (int i = 0; i < 17; i++) {
            JLabel imageLable = new JLabel(Util.getIcon("/img/" + player1.get(i).getPicture() + ".jpg"));
            imageLable.setBounds(40, y, 105, 150);
            y -= 30;
            this.add(imageLable);
        }
        y = 600;
        for (int i = 0; i < 17; i++) {
            JLabel imageLable = new JLabel(Util.getIcon("/img/" + player2.get(i).getPicture() + ".jpg"));
            imageLable.setBounds(850, y, 105, 150);
            y -= 30;
            this.add(imageLable);
        }
        int x = 670;
        for (int i = 0; i < 17; i++) {
            JLabel imageLable = new JLabel(Util.getIcon("/img/" + player3.get(i).getPicture() + ".jpg"));
            imageLable.setBounds(x, 780, 105, 150);
            x -= 30;
            this.add(imageLable);
        }
        x = 500;
        for (int i = 0; i < 3; i++) {
            JLabel imageLable = new JLabel(Util.getIcon("/img/" + base.get(i).getPicture() + ".jpg"));
            imageLable.setBounds(x, 20, 105, 150);
            x -= 30;
            this.add(imageLable);
        }
        this.setSize(1000, 1000);
        this.setTitle("斗地主");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);


    }

    public void init() {
        String[] points = "3,4,5,6,7,8,9,10,J,Q,K,A,2".split(",");
        String[] types = "♠,♥,♣,♦".split(",");
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < types.length; j++) {
                pukes.add(new Card(points[i], types[j], i, j, (i + 3) + "_" + (j + 1)));
            }
        }
        pukes.add(new Card("大王", "", 14, 0, "17_1"));
        pukes.add(new Card("小王", "", 13, 0, "16_1"));

    }

    public void xipai() {
        Collections.shuffle(pukes);
        System.out.println(pukes);
    }

    public void fapai() {
        player1 = pukes.subList(0, 17);
        player2 = pukes.subList(17, 34);
        player3 = pukes.subList(34, 51);
        base = pukes.subList(51, 54);
        sort(player1);
        sort(player2);
        sort(player3);
        sort(base);
        System.out.println("玩家一：" + player1);
        System.out.println("玩家二：" + player2);
        System.out.println("玩家三：" + player3);
        System.out.println("底牌：" + base);
    }

    public void sort(List list) {
        list.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getPointValue() == o2.getPointValue()) {
                    return o1.getTypeValue() - o2.getTypeValue();
                } else {
                    return o1.getPointValue() - o2.getPointValue();
                }
            }
        });
    }


    public static void main(String[] args) {
        new FightTheLandlord();
    }
}
